import { Http } from "@/utils/request";
import { Toast } from "@/utils/toast";
import { delayTask } from "@/utils/timer";
import type { AppDataUserData } from "@/utils/app";

const baseUrl = "/guider_home";

export const init_index_api = (showError = true) => {
  return Http.get<InitIndex>(`${baseUrl}/init/index`).catch((error: Error) => {
    showError && delayTask(60, () => Toast.show(error.message));
    return Promise.reject(error);
  });
};

export interface InitIndex {
  about_text: string; // 关于我们
  user_agreement: string; // 用户协议
  privacy_agreement: string; // 隐私政策
  security_center: string; // 安全中心
  // help_h5_url: string; // 帮助中心
  help_list: { id: string; title: string }[]; // 帮助中心问题
  op_shop_url: string; // 开店客服
  guider_certification: string; // 导游宝认证引导页
  share_info: { title: string };
}

export const home_index_api = (showError = true) => {
  return Http.get<{
    banner: any[];
    icon: any[];
    auth_guider: any;
    live: any[];
    menu_title: any[];
    industry_news: any[];
    guider_interview: any[];
    task: any[];
    task_right: any[];
    user_task: any[];
    user_task_right: any[];
  }>(`${baseUrl}/home/index`).catch((error: Error) => {
    showError && delayTask(60, () => Toast.show(error.message));
    return Promise.reject(error);
  });
};

export const get_guide_certification_audit_api = (showError = true) => {
  return Http.get<{ status: number; text: string }>(
    `${baseUrl}/get_guide/certification_audit`
  ).catch((error: Error) => {
    showError && delayTask(60, () => Toast.show(error.message));
    return Promise.reject(error);
  });
};

export const search_keyword_api = (showError = true) => {
  return Http.get<{ keyword_arr: string[] }>(`${baseUrl}/search/keyword`).catch(
    (error: Error) => {
      showError && delayTask(60, () => Toast.show(error.message));
      return Promise.reject(error);
    }
  );
};

export const search_api = (data: { keyword: string }, showError = true) => {
  return Http.get<{
    all_count: number;
    course: { items: any[] };
    shop: { items: any[] };
    article: { items: any[] };
    note: { items: any[] };
  }>(`${baseUrl}/search/search`, data).catch((error: Error) => {
    showError && delayTask(60, () => Toast.show(error.message));
    return Promise.reject(error);
  });
};

export const guider_study_index_api = (showError = true) => {
  return Http.get<{ banner: any[]; icon: any[]; exam_zone: any[] }>(
    `${baseUrl}/guider_study/index`
  ).catch((error: Error) => {
    showError && delayTask(60, () => Toast.show(error.message));
    return Promise.reject(error);
  });
};

export const user_info_api = (showError = true) => {
  return Http.get<any>(`${baseUrl}/user/info`).catch((error: Error) => {
    showError && delayTask(60, () => Toast.show(error.message));
    return Promise.reject(error);
  });
};

export const login_mini_login_api = (
  data: { code: string; mobile: any },
  showError = true
) => {
  return Http.post<AppDataUserData>(`${baseUrl}/login/mini_login`, data).catch(
    (error: Error) => {
      showError && delayTask(60, () => Toast.show(error.message));
      return Promise.reject(error);
    }
  );
};

export const login_user_account_api = (
  data: { phone: string; password: string },
  showError = true
) => {
  return Http.post<AppDataUserData>(
    `${baseUrl}/login/user_account`,
    data
  ).catch((error: Error) => {
    showError && delayTask(60, () => Toast.show(error.message));
    return Promise.reject(error);
  });
};

export const login_reset_password_api = (
  data: { phone: string; password: string; password1: string; code: string },
  showError = true
) => {
  return Http.post(`${baseUrl}/login/reset_password`, data).catch(
    (error: Error) => {
      showError && delayTask(60, () => Toast.show(error.message));
      return Promise.reject(error);
    }
  );
};

export const community_index_api = (showError = true) => {
  return Http.get<{
    banner: any[];
    guider_interview: any[];
    hot_group: any[];
    menu_title: any[];
    guider_share: any[];
  }>(`${baseUrl}/community/index`).catch((error: Error) => {
    showError && delayTask(60, () => Toast.show(error.message));
    return Promise.reject(error);
  });
};

export const community_note_list_api = (
  data: { cate_id: string } & MiniApp.PagingQuery,
  showError = true
) => {
  return Http.get<MiniApp.PagingData>(
    `${baseUrl}/community/note_list`,
    data
  ).catch((error: Error) => {
    showError && delayTask(60, () => Toast.show(error.message));
    return Promise.reject(error);
  });
};

export const guider_cash_index_api = (showError = true) => {
  return Http.get<{
    banner: any[];
    menu_title: any[];
    agency_demand: any[];
    agency_menu_pic: string;
    agency_pics: any[];
    custom_demand: any[];
    custom_menu_pic: string;
    custom_pics: any[];
    open_shop: {
      video_url: string;
      video_pic: string;
      shop_example: any[];
    };
    shop_bg_pic: string;
    guider_explain: any;
  }>(`${baseUrl}/guider_cash/index`).catch((error: Error) => {
    showError && delayTask(60, () => Toast.show(error.message));
    return Promise.reject(error);
  });
};
